import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:qqjdflutter/page/cart_page.dart';
import 'package:qqjdflutter/page/category.dart';
import 'package:qqjdflutter/page/home.dart';
import 'package:qqjdflutter/page/user.dart';
import 'package:qqjdflutter/provider/bottom_navi_provider.dart';

class IndexPage extends StatefulWidget {
  const IndexPage({Key? key}) : super(key: key);

  @override
  _IndexPageState createState() => _IndexPageState();
}

class _IndexPageState extends State<IndexPage> {
  int _currentIndex = 0;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      bottomNavigationBar: Consumer<BottomNaviProvider>(
        builder: (_,mProvider,__){
          return BottomNavigationBar(
            type: BottomNavigationBarType.fixed,
            currentIndex: mProvider.bottomNaviIndex,
            items: [
              BottomNavigationBarItem(icon: Icon(Icons.home),label: "首页"),
              BottomNavigationBarItem(icon: Icon(Icons.category),label:"分类"),
              BottomNavigationBarItem(icon: Icon(Icons.shopping_cart),label:"购物车"),
              BottomNavigationBarItem(icon: Icon(Icons.account_circle),label:"我的"),
            ],
            onTap: (index){
              //print(index);
              // setState(() {
              //   _currentIndex = index;
              // });
              mProvider.changeBottomNaviIndex(index);
            },
          );
        },
      ),
      body: Consumer<BottomNaviProvider>(builder: (_,mProvider,__)=>
          IndexedStack(
            index: mProvider.bottomNaviIndex,// 布局控件 只显示一个
            children:<Widget> [
              HomePage(),
              CategoryPage(),
              CartPage(),
              UserPage(),
            ],

          ),
      )
    );
  }
}
